﻿var app = angular.module('app', []);

/*
app.factory('LectureList', function ($scope, $http) {
    return {
        list: [
            {
                id: 0,
                name: "Lecture 1",
                link: "http://www.youtube.com/embed/uYZy9eRazVw",
                comments: ["hello", "ja", "saell"]
            }, {
                id: 1,
                name: "Lecture 2",
                link: "http://www.youtube.com/embed/rrOuJY3G5fU",
                comments: ["va", "shitt thetta er langt comment, veit ekki hvort thad virki en samt va...!!!!"]
            }
        ]
    };

});
*/
app.config(function ($routeProvider) {
    $routeProvider
		.when('/',
		{ templateUrl: "lectures.html", controller: "LecturesController" })
		.when('/lecture/:id',
		{ templateUrl: "lecture.html", controller: "LectureController" })
		.otherwise(
			{ template: "End of the multiverse!!!!" })
    ;
});




app.controller('CommentController', ['$scope', '$http', function ($scope, $http) {
    $scope.comment = {};
   
    $scope.submit = function () {
        console.log($scope);
        $http({
            method: 'POST',
            url: "/api/lectures/" + $scope.lecture.ID + "/comments",
            data: JSON.stringify({ commentText: $scope.commentText })
        }).success(function () {
            console.log($scope.commentText);
            $http.get("/api/lectures/" + $scope.lecture.ID + "/comments").success(function (data, status) {
                $scope.comments = null;
                $scope.comments = data;    
            });
        });
    };
        
        
}]);


app.controller('LecturesController', ["$location", "$http", '$scope', function ($location, $http, $scope) {
    $scope.lectures = {};
    $scope.lecture={};
    $http.get("/api/lectures").success(function (data, status) {
        $scope.lectures = data;
    });
    $scope.submit = function () {
        console.log($scope);

        $http({
            method: 'POST',
            url: "/api/lectures/",
            data: JSON.stringify({ Name: $scope.Name, LectureURL: $scope.LectureURL, Description: $scope.Description })
        }).success(function () {
        });

    };

}]);

app.controller('LectureController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
 
    var lecture;
    $http.get("/api/lectures/" + $routeParams.id).success(function (data, status) {
        $scope.lecture = data;
    });
    $http.get("/api/lectures/" + $routeParams.id+"/comments").success(function (data, status) {
        $scope.comments = data;
    });
    /*for (var i = 0; i<LectureList.list.length;i++){
        if (LectureList.list[i].id == $routeParams.id){
            lecture = LectureList.list[i];
            break;
        }
    }*/
    
}]);

$('.dropdown-toggle').dropdown()
